package <%=packageName%>.web.rest.util;

import org.springframework.beans.BeanUtils;
import org.springframework.data.jpa.repository.JpaRepository;

import java.beans.PropertyDescriptor;
import java.io.Serializable;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class PatchUtil {
    public static <T extends Serializable, R extends JpaRepository<T, Long>> void save(R r, T t, T tmpEntity) {
        PropertyDescriptor[] descriptors = BeanUtils.getPropertyDescriptors(t.getClass());
        try {
            for (PropertyDescriptor descriptor : descriptors) {
                Method readMethod = descriptor.getReadMethod();
                Object value = readMethod.invoke(tmpEntity);
                    if (value != null) {
                        Method writeMethod = descriptor.getWriteMethod();
                        if (writeMethod != null) {
                            writeMethod.invoke(t, value);
                            r.save(t);
                        }
                    }
            }
        } catch (IllegalAccessException | InvocationTargetException e) {
            e.printStackTrace();
        }
    }
}
